﻿#region Using Directives

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using QuickDev.Common.Audit;
using QuickDev.Localization;
using QuickDev.Model;

#endregion

namespace QuickDev.Web.App.Administration.Controls.SiteSettingConfiguration
{
    public partial class images : SiteSettingsControl
    {
        #region Page Events

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected override void Page_Load( object sender, EventArgs e )
        {
            try
            {
                base.Page_Load( sender, e );
                if ( view.Equals( "i" ) )
                {
                    SetImageSettingsProperties();
                    if ( !Page.IsPostBack )
                    {
                        chkGenerateThumbs.Checked = SiteSettings.GenerateThumbs;
                        txtSmallWidth.Text = SiteSettings.ThumbSmallWidth.ToString();
                        txtSmallHeight.Text = SiteSettings.ThumbSmallHeight.ToString();

                        txtSystemThumbnailHeight.Text = SiteSettings.SystemThumbnailHeight.ToString();
                        txtSystemThumbnailWidth.Text = SiteSettings.SystemThumbnailWidth.ToString();
                    }
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( general ).Name + ".Page_Load", ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="T:System.EventArgs"/> instance containing the event data.</param>
        protected void btnSave_Click( object sender, EventArgs e )
        {
            try
            {
                SiteSettings.GenerateThumbs = chkGenerateThumbs.Checked;
                SiteSettings.ThumbSmallWidth = int.Parse( txtSmallWidth.Text );
                SiteSettings.ThumbSmallHeight = int.Parse( txtSmallHeight.Text );

                SiteSettings.SystemThumbnailWidth = int.Parse( txtSystemThumbnailWidth.Text );
                SiteSettings.SystemThumbnailHeight = int.Parse( txtSystemThumbnailHeight.Text );

                ServiceConfiguration model = base.SaveConfiguration( SiteSettings );
                if ( model.ConfigurationId > 0 )
                {
                    MasterPage.MessageCenter.DisplaySuccessMessage( LocalizationUtility.GetText( "lblSiteSettingsSaved" ) );
                }
                else
                {
                    MasterPage.MessageCenter.DisplayFailureMessage( LocalizationUtility.GetText( "lblSiteSettingsNotSaved" ) );
                }
            }
            catch ( Exception ex )
            {
                Logger.Error( typeof( images ).Name + ".btnSave_Click", ex );
                base.MasterPage.MessageCenter.DisplayCriticalMessage( ex.Message );
            }
        }

        #endregion

        #region Methods

        #region Private

        /// <summary>
        /// Sets the general settings properties.
        /// </summary>
        private void SetImageSettingsProperties()
        {
            this.Page.Title = string.Format( WebUtility.MainTitleTemplate, base.MasterPage.SiteSettings.SiteName, LocalizationUtility.GetText( "titleSiteSettingsImages" ) );
        }

        #endregion

        #endregion
    }
}